'''
@Project ：12306
@File    ：email.py
@IDE     ：PyCharm 
@Author  ：子协
@Date    ：2025-04-30 11:55 
'''
import smtplib
from email.mime.text import MIMEText
import importlib,sys

class email:
    def __init__(self,name,account,token):
        self.name = name
        self.account = account
        self.token = token

    def login(self):
        self.smtp = smtplib.SMTP_SSL('smtp.qq.com', 465)
        try:
            res = self.smtp.login(self.account, self.token)
            if res[0] == 235:
                # print("登录成功")
                return 0
        except:
            # print("登录失败")
            return -1

    def quit(self):
        res = self.smtp.quit()
        if res[0] == 221:
            # print("退出成功")
            return 0
        else:
            # print("退出失败")
            return -1

    def sendText(self,to_name,to_email,topic,msg):
        email_content = MIMEText(msg, 'plain', 'utf-8')
        email_content['From'] = f"{self.name} <{self.account}>"  # 发送者昵称
        email_content['To'] = f"{to_name} <{to_email}>"          # 接收者昵称
        email_content['Subject'] = topic                        # 主题
        try:
            self.smtp.sendmail(self.account, to_email, email_content.as_string())
            # print("发送成功")
            return 0
        except Exception as e:
            # print("发送失败")
            print(e)
            return -1
